(in-package :stumpwm)

;;set contrib directory
;;(set-contrib-dir "/home/ragnarg/lisp/stumpwm/contrib")
(set-contrib-dir "/home/ragnarg/stumpwm/contrib")
;;load a few modules
(mapcar #'load-module '("cpu"
			"mem"
;;			"battery"
;;			"battery-portable"
;;			"net"
;;			"wifi"
			"disk"))


(defun battery-left  () (let ((in (open "/sys/devices/LNXSYSTM:00/device:00/PNP0A08:00/PNP0C0A:00/power_supply/BAT0/capacity")))
 (car (list (parse-integer (format nil "~d" (read-line in))) (close in)))))

;;/sys/devices/pci0000:00/0000:00:02.0/backlight/acpi_video1

;;sys/class/power_supply/AC0/online
(defun is-unplugged  () (let ((in (open "/sys/devices/LNXSYSTM:00/device:00/PNP0A08:00/ACPI0003:00/power_supply/AC0/online")))
 (list (= 0 (parse-integer (format nil "~d" (read-line in)))) (close in))))

(let ((litur "^B^61"))
  (defun toggle-litur ()
    (setq litur
          (if (string= litur "^B^16")
              "^B^61"
              "^B^16"))))

(defun colormebar ()
  (if (and (car (is-unplugged)) (> 5 (battery-left)))
      (progn
	(run-shell-command "play /usr/share/sounds/gnome/default/alerts/drip.ogg")
	(toggle-litur))
      "^7*"))


(setf *mode-line-background-color* "darkblue"
      *mode-line-border-color* "blue"
      *mode-line-foreground-color* "forestgreen"
      *mode-line-border-width* 4        ; "How thick shall the mode line border be?"
      *mode-line-pad-x* 10
      *mode-line-pad-y* 1
      *mode-line-timeout* 5)           ; Update every 10 seconds if nothing else has triggered it already.
(unless (boundp '*day-names*)
  (defconstant *day-names*
    '("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun")))

(defun bat-to-modeline ( powerleft ) 
  (cond 
    ((< powerleft 5) ( concat "^1 " (write-to-string powerleft)))
    ((< powerleft 15) ( concat "^2 " (write-to-string powerleft)))
    ((< powerleft 25) ( concat "^3 " (write-to-string powerleft)))
    ((< powerleft 50) ( concat "^4 " (write-to-string powerleft)))
    ((< powerleft 75) ( concat "^5 " (write-to-string powerleft)))
    (( concat "^7 " (write-to-string powerleft)))))

(defun helper-batt ()
  (bat-to-modeline (battery-left)))

(defun current-date ()
  "Returns the date formatted as `2009-11-16 Mon 11:34:03'."
  (multiple-value-bind
	(second minute hour date month year day-of-week dst-p tz)
      (get-decoded-time)
    (format nil "~d-~2,'0d-~2,'0d ~a ^B~2,'0d:~2,'0d:~2,'0d^b"
            year month date (nth day-of-week *day-names*)
            hour minute second)))

(setf *screen-mode-line-format* (list 
				 " ^7*" '(:eval (current-date))
				 " ^1*(^[^nu ^B^5*%u^])^n"
				 " ^2*(^[^ng ^]%g)^n"
				 "  | %D | %c %C (%f) %t | %M | " '(:eval (colormebar))
				 '(:eval (helper-batt))
				  " rafhlaða"
				 ))


(mode-line)




